package jcataloger.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.IOException;
import java.net.URL;
import java.util.Stack;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import jcataloger.resources.Messages;
import jcataloger.resources.Parameters;

/**
 * This class contains the help document of the project. It presents in a JEditorPane.
 * @author sekobey
 *
 */
public class HelpFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel contentPaneFrame = null;

	private JPanel panelNorth = null;

	private JButton buttonBack = null;

	private JButton buttonForward = null;

	private JEditorPane editorPane = null;

	private JScrollPane jScrollPane = null;
	
	private Stack<URL> stackUrlBack;
	
	private Stack<URL> stackUrlNext;

	/**
	 * This method initializes panelNorth	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelNorth() {
		if (panelNorth == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			panelNorth = new JPanel();
			panelNorth.setLayout(flowLayout);
			panelNorth.add(getButtonBack(), null);
			panelNorth.add(getButtonForward(), null);
		}
		return panelNorth;
	}

	/**
	 * This method initializes buttonBack	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getButtonBack() {
		if (buttonBack == null) {
			buttonBack = new JButton();
			ClassLoader loader = this.getClass().getClassLoader();
			buttonBack.setIcon(new ImageIcon(loader.getResource(Parameters.getString("icons32x32.back"))));
			buttonBack.setEnabled(false);
			buttonBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (stackUrlBack.size() > 1) {
						URL url = stackUrlBack.pop();
						stackUrlNext.push(url);
						try {
							editorPane.setPage(stackUrlBack.peek());
						} catch (IOException e1) {
							e1.printStackTrace();
						}
						if (stackUrlBack.size() == 1)
							buttonBack.setEnabled(false);
						buttonForward.setEnabled(true);
					}
				}
			});
		}
		return buttonBack;
	}

	/**
	 * This method initializes buttonForward	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getButtonForward() {
		if (buttonForward == null) {
			buttonForward = new JButton();
			ClassLoader loader = this.getClass().getClassLoader();
			buttonForward.setIcon(new ImageIcon(loader.getResource(Parameters.getString("icons32x32.forward"))));
			buttonForward.setEnabled(false);
			buttonForward.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (stackUrlNext.size() > 0) {
						URL url = stackUrlNext.pop();
						stackUrlBack.push(url);
						try {
							editorPane.setPage(url);
						} catch (IOException e1) {
							e1.printStackTrace();
						}
						if (stackUrlNext.size() == 0)
							buttonForward.setEnabled(false);
						buttonBack.setEnabled(true);
					}
				}
			});
		}
		return buttonForward;
	}

	/**
	 * This method initializes editorPane	
	 * 	
	 * @return javax.swing.JEditorPane	
	 */
	private JEditorPane getEditorPane() {
		if (editorPane == null) {
			editorPane = new JEditorPane();
			editorPane.setEditable(false);
			editorPane.setContentType("text/html"); //$NON-NLS-1$
			editorPane.setEditorKit(new FixedHTMLEditorKit());
			try {
				ClassLoader loader = this.getClass().getClassLoader();
				URL initialPage = loader.getResource("jcataloger/gui/help.html");
				stackUrlBack.push(initialPage);
				editorPane.setPage(initialPage);
				editorPane.addHyperlinkListener(
						new HyperlinkListener() {
							public void hyperlinkUpdate(HyperlinkEvent e) {
								if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED ) {
									try {
										// remember URL for back button
										stackUrlBack.push(e.getURL());
										buttonBack.setEnabled(true);
										editorPane.setPage(e.getURL());
									} catch (IOException e1) {
										e1.printStackTrace();
									}
								}
							}
						}
				);
			} catch (IOException e) {
				editorPane.setText(Messages.getString("HelpFrame.0")); //$NON-NLS-1$
			}
		}
		return editorPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getEditorPane());
		}
		return jScrollPane;
	}

	/**
	 * This is the default constructor
	 */
	public HelpFrame(JFrame owner) {
		super();
		stackUrlBack = new Stack<URL>();
		stackUrlNext = new Stack<URL>();
		initialize(owner);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize(JFrame owner) {
		this.setSize(640, 480);
		this.setContentPane(getContentPaneFrame());
		this.setLocationRelativeTo(owner);
		this.setTitle(Messages.getString("HelpFrame.1")); //$NON-NLS-1$
	}

	/**
	 * This method initializes contentPaneFrame
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getContentPaneFrame() {
		if (contentPaneFrame == null) {
			contentPaneFrame = new JPanel();
			contentPaneFrame.setLayout(new BorderLayout());
			contentPaneFrame.add(getPanelNorth(), BorderLayout.NORTH);
			contentPaneFrame.add(getJScrollPane(), BorderLayout.CENTER);
		}
		return contentPaneFrame;
	}

	
	
}
